/*
 * Copyright (C) 2020 UNISOC Open Source Project
 *
 */

package com.unisoc.systemui.superpower;

import android.content.Context;
import android.os.RemoteException;
import android.os.sprdpower.PowerManagerEx;
import android.os.sprdpower.IPowerManagerEx;
import android.os.ServiceManager;
import android.provider.Settings;
import android.util.Log;

import java.util.List;

/* UNISOC:BUG 1074234 add for Super power feature */
public class UnisocPowerManagerUtil {

    private static final String TAG = "UnisocPowerManagerUtil";

    public static final int MODE_INVALID = PowerManagerEx.MODE_INVALID;//-1
    public static final int MODE_PERFORMANCE = PowerManagerEx.MODE_PERFORMANCE;//0
    public static final int MODE_SMART = PowerManagerEx.MODE_SMART;//1
    public static final int MODE_POWERSAVING = PowerManagerEx.MODE_POWERSAVING;//2
    public static final int MODE_LOWPOWER = PowerManagerEx.MODE_LOWPOWER;//3
    public static final int MODE_ULTRASAVING = PowerManagerEx.MODE_ULTRASAVING;//4
    public static final int MODE_NONE = PowerManagerEx.MODE_NONE;//5
    public static final int MODE_MAX = MODE_NONE;
    public static final String ACTION_POWEREX_SAVE_MODE_CHANGED = PowerManagerEx.ACTION_POWEREX_SAVE_MODE_CHANGED;
    public static final String EXTRA_POWEREX_SAVE_MODE = PowerManagerEx.EXTRA_POWEREX_SAVE_MODE;
    public static final String EXTRA_POWEREX_SAVE_PREMODE = PowerManagerEx.EXTRA_POWEREX_SAVE_PREMODE;
    public static final String ASSISTANT_ON = Settings.Secure.ASSISTANT_ON;//"assistant_on"
    public static final String ASSISTANT_OPENBEFORE_ON = Settings.Secure.ASSISTANT_OPENBEFORE_ON;//"assistant_openbefore_on"
    public static final String COLORINVERSION_OPENBEFORE_ON = Settings.Secure.COLORINVERSION_OPENBEFORE_ON;//"colorinversion_openbefore_on"

    public static final boolean SUPPORT_SUPER_POWER_SAVE = (1 == android.os.SystemProperties.getInt("ro.sys.pwctl.ultrasaving",0));

    private static IPowerManagerEx mPowerManagerEx;

    public static IPowerManagerEx getPowerManagerEx() {
        if (mPowerManagerEx == null) {
            mPowerManagerEx = IPowerManagerEx.Stub.asInterface(ServiceManager.getService("power_ex"));
        }
        return mPowerManagerEx;
    }

    public static int getPowerSaveModeInternal() {
        try {
            int mode = getPowerManagerEx().getPowerSaveMode();
            return mode;
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return -1;
    }

    public static boolean isSuperPower() {
        return SUPPORT_SUPER_POWER_SAVE && (getPowerSaveModeInternal() == MODE_ULTRASAVING);
    }

    public static boolean setPowerSaveMode(int mode) {
        Log.d(TAG, "setPowerSaveMode mode=" + mode);
        try {
            boolean result = getPowerManagerEx().setPowerSaveMode(mode);
            return result;
        } catch (RemoteException e) {
            e.printStackTrace();
        } catch (NullPointerException e) {
            e.printStackTrace();
        }
        return false;
    }

    public static boolean getSmartSavingModeWhenCharging() {
        final IPowerManagerEx powerManagerEx = getPowerManagerEx();
        boolean result = false;
        try {
            if (powerManagerEx != null) {
                result = powerManagerEx.getSmartSavingModeWhenCharging();
            }
        } catch (RemoteException e) {
            Log.e(TAG,"getSmartSavingModeWhenCharging error");
        }
        return result;
    }

    public static List<String> getAllowedAppListInUltraSavingMode() {
        final IPowerManagerEx powerManagerEx = getPowerManagerEx();
        List<String> result = null;
        try {
            if (powerManagerEx != null) {
                result = powerManagerEx.getAllowedAppListInUltraSavingMode();
            }
        } catch (RemoteException e) {
            Log.e(TAG,"getAllowedAppListInUltraSavingMode error");
        }
        return result;
    }
/*
    class PowerManagerEx {
        public static final int MODE_INVALID = -1;//PowerManagerEx.MODE_INVALID;//-1
        public static final int MODE_PERFORMANCE = 0;//PowerManagerEx.MODE_PERFORMANCE;//0
        public static final int MODE_SMART = 1;//PowerManagerEx.MODE_SMART;//1
        public static final int MODE_POWERSAVING = 2;//PowerManagerEx.MODE_POWERSAVING;//2
        public static final int MODE_LOWPOWER = 3;//PowerManagerEx.MODE_LOWPOWER;//3
        public static final int MODE_ULTRASAVING =4;// PowerManagerEx.MODE_ULTRASAVING;//4
        public static final int MODE_NONE = 5;//PowerManagerEx.MODE_NONE;//5
        public static final int MODE_MAX = MODE_NONE;

        public static final String ACTION_POWEREX_SAVE_MODE_CHANGED = "ACTION_POWEREX_SAVE_MODE_CHANGED";
        public static final String EXTRA_POWEREX_SAVE_MODE = "EXTRA_POWEREX_SAVE_MODE";
        public static final String EXTRA_POWEREX_SAVE_PREMODE = "EXTRA_POWEREX_SAVE_PREMODE";

    }
    class IPowerManagerEx {
        public boolean setPowerSaveMode(int mode) {
            return false;
        }
        public int getPowerSaveMode() {
            return 0;
        }
        public boolean getSmartSavingModeWhenCharging() {
            return false;
        }
        public List<String> getAllowedAppListInUltraSavingMode() {
            return null;
        }
    }
*/

}
